package 力扣_算法题综合.高频面试算法;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author zx
 * @create 2023-02-24 10:51
 */
public class 用栈实现队列_232 {
    class MyQueue {
        Deque<Integer> inStack;
        Deque<Integer> outStack;
        public MyQueue() {
            inStack = new ArrayDeque<>();
            outStack = new ArrayDeque<>();
        }

        public void push(int x) {
            inStack.push(x);
        }

        public int pop() {
            if(outStack.isEmpty()){
                while(!inStack.isEmpty()){
                    outStack.push(inStack.pop());
                }
            }
            return outStack.pop();
        }

        /**
         * pop()和peek()的方法几乎是一样的
         */
        public int peek() {
            if(outStack.isEmpty()){
                while(!inStack.isEmpty()){
                    outStack.push(inStack.pop());
                }
            }
            return outStack.peek();
        }

        public boolean empty() {
            return inStack.isEmpty() && outStack.isEmpty();
        }
    }
}
